<?php
/**
 * BasicTable Class
 *
 * This class makes it easy to take a one-dimensional list of HTML chunks, and
 * arrange the chunks in a table with the specified number of columns. Chunks
 * will be put into the table in a left-to-right/top-to-bottom fashion.
 *
 * @author Michael Thomas <michael.thomas@gmail.com>
 * @version 6.6.2008
 * @package cs
 */

class BasicTable
{
  protected $columns;
  protected $items = array();
  protected $tableAttributes = array();
  protected $rowAttributes = array();
  protected $cellAttributes = array();

  public function __construct($columns)
  {
    $this->columns = $columns;
  }

  public function addItem($string)
  {
    $this->items[] = $string;
  }

  public function setTableAttribute($attribute, $value)
  {
    $this->tableAttributes[$attribute] = $value;
  }

  public function setCellAttribute($attribute, $value)
  {
    $this->cellAttributes[$attribute] = $value;
  }

  public function show()
  {
    $items = $this->items;
    $rows = ceil(count($this->items) / $this->columns);
    $this->showTableTag();
    for ($row = 0; $row < $rows; $row++) {
      $this->showRowTag();
      for ($column = 0; $column < $this->columns; $column++) {
        $this->showCellTag();
        if (count($items) > 0) {
          echo array_shift($items);
        }
        echo "</td>\n";
      }
      echo "</tr>\n";
    }
    echo "</table>\n";
  }

  protected function showTableTag()
  {
    echo "<table";
    foreach ($this->tableAttributes as $name => $value) {
      echo " $name=\"$value\"";
    }
    echo ">\n";
  }

  protected function showRowTag()
  {
    echo "<tr";
    foreach ($this->rowAttributes as $name => $value) {
      echo " $name=\"$value\"";
    }
    echo ">\n";
  }

  protected function showCellTag()
  {
    echo "<td";
    foreach ($this->cellAttributes as $name => $value) {
      echo " $name=\"$value\"";
    }
    echo ">\n";
  }
}
?>